<template>
<div class="app-container calendar-list-container">
  <div class="filter-container">
        <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="bean名称" v-model="listQuery.beanName"> </el-input>        
        <el-button class="filter-item" type="primary" v-waves icon="search" @click="handleFilter">查询</el-button>
        <el-button class="filter-item"  style="margin-left: 10px;" @click="handleCreate" type="primary" icon="plus">新增</el-button>
        <router-link :to="{ path:'jobLog'}">
        <el-button class="filter-item" type="primary" v-waves>日志列表</el-button></router-link>
      </div>
  <el-table :key='tableKey' :data="list" v-loading.body="listLoading" border fit highlight-current-row style="width: 100%">
        <el-table-column align="center" label="任务ID" width="200"> <template scope="scope">
              <span>{{scope.row.jobId}}</span>
        </template> </el-table-column>
        <el-table-column width="120" align="center" label="bean名称"> <template scope="scope">
            <span>{{scope.row.beanName}}</span>
        </template> </el-table-column>
        <el-table-column width="100" align="center" label="方法名称"> <template scope="scope">
                <span>{{scope.row.methodName}}</span>
        </template> </el-table-column>
        <el-table-column width="150" align="center" label="参数"> <template scope="scope">
              <span>{{scope.row.params}}</span>
        </template> </el-table-column>
        <el-table-column width="190" align="center" label="cron表达式"> <template scope="scope">
                <span>{{scope.row.cronExpression}}</span>
        </template> </el-table-column>
        <el-table-column width="190" align="center" label="备注"> <template scope="scope">
                <span>{{scope.row.remark}}</span>
              </template> </el-table-column>
        <el-table-column width="90" align="center" label="状态"> <template scope="scope">
              <span v-if="scope.row.status == 0">正常</span>
              <span v-else>暂停</span>
        </template> </el-table-column>
        <el-table-column align="center" label="操作" width="600px"> 
          <template scope="scope">
               <el-button v-if="scope.row.status == 0" size="small" type="success" @click="handlePause(scope.row)">暂停</el-button>
               <el-button v-if="scope.row.status == 1" size="small" type="warning" @click="handleResume(scope.row)">恢复</el-button>
               <el-button  size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
               <el-button v-if="scope.row.status == 1" size="small" type="info" @click="handleUpdate(scope.row)">修改</el-button>
          </template> 
        </el-table-column>
  </el-table>
  <div v-show="!listLoading" class="pagination-container">
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
  </div>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
    <el-form :model="form" :rules="rules" ref="form" label-width="150px">
      <el-form-item label="bean名称" prop="beanName">
        <el-input  v-model="form.beanName" placeholder="spring bean名称，如：testTask"></el-input>
      </el-form-item>
      <el-form-item label="方法名称" prop="methodName">
         <el-input  v-model="form.methodName" placeholder="方法名称"></el-input>
      </el-form-item>
      <el-form-item label="参数" prop="params">
         <el-input  v-model="form.params" placeholder="参数"></el-input>
      </el-form-item>
      <el-form-item label="cron表达式" prop="cronExpression">
         <el-input  v-model="form.cronExpression" placeholder="如：0 0 12 * * ?"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input  placeholder="备注" v-model="form.remark"> </el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel('form')">取 消</el-button>
      <el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')">确 定</el-button>
      <el-button v-else type="primary" @click="update('form')">确 定</el-button>
    </div>
  </el-dialog>

</div>
</template>

<script>
import {
  page,
  addObj,
  getObj,
  delObj,
  putObj,
  // getInfo,
  // runTask,
  pauseObj,
  resumeObj
} from '@/api/biz/operative/job/index'
import { mapGetters } from 'vuex'
export default {
  name: 'job',
  data() {
    return {
      form: {
        jobId: '',
        beanName: '',
        methodName: '',
        params: '',
        cronExpression: '',
        remark: ''
      },
      rules: {
        beanName: [
          {
            required: true,
            message: '请输入bean名称',
            trigger: 'blur'
          }
        ],
        methodName: [
          {
            required: true,
            message: '请输入方法名称',
            trigger: 'blur'
          }
        ],
        cronExpression: [
          {
            required: true,
            message: '表达式必输',
            trigger: 'blur'
          }
        ]
      },
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        beanName: ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      userManager_btn_add: false,
      textMap: {
        update: '修改',
        create: '新增'
      },
      tableKey: 0
    }
  },
  created() {
    this.getList()
  },
  computed: {
    ...mapGetters([
      'elements'
    ])
  },
  methods: {
    getList() {
      this.listLoading = true
      page(this.listQuery)
        .then(response => {
          this.list = response.data.rows
          console.log(this.list)
          this.total = response.data.total
          this.listLoading = false
        })
    },
    handleFilter() {
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    handlePause(row) {
      this.$confirm('此操作将任务暂停, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          pauseObj(row)
            .then(() => {
              this.$notify({
                title: '成功',
                message: '任务暂停成功',
                type: 'success',
                duration: 2000
              })
              this.getList()
            })
        })
    },
    handleUpdate(row) {
      getObj(row)
        .then(response => {
          console.log(response)
          this.dialogFormVisible = true
          this.dialogStatus = 'update'
          this.form = response.data
        })
    },
    handleResume(row) {
      resumeObj(row)
        .then(() => {
          this.$notify({
            title: '成功',
            message: '任务恢复成功',
            type: 'success',
            duration: 2000
          })
          this.getList()
        })
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          delObj(row)
            .then(() => {
              this.$notify({
                title: '成功',
                message: '删除成功',
                type: 'success',
                duration: 2000
              })
              const index = this.list.indexOf(row)
              this.list.splice(index, 1)
            })
        })
    },
    create(formName) {
      const set = this.$refs
      set[formName].validate(valid => {
        if (valid) {
          console.log(this.form)
          this.$confirm('此操作将添加任务, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
            .then(() => {
              addObj(this.form)
                .then(() => {
                  this.dialogFormVisible = false
                  this.getList()
                  this.$notify({
                    title: '成功',
                    message: '新增成功',
                    type: 'success',
                    duration: 2000
                  })
                })
            })
        } else {
          return false
        }
      })
    },
    cancel(formName) {
      this.dialogFormVisible = false
      this.$refs[formName].resetFields()
    },
    update(formName) {
      const set = this.$refs
      set[formName].validate(valid => {
        if (valid) {
          putObj(this.form).then(() => {
            this.dialogFormVisible = false
            this.getList()
            this.$notify({
              title: '成功',
              message: '修改成功',
              type: 'success',
              duration: 2000
            })
          })
        } else {
          return false
        }
      })
    },
    resetTemp() {
      this.dialogStatus = ''
      this.form = {
        jobId: '',
        beanName: '',
        methodName: '',
        params: '',
        cronExpression: '',
        remark: ''
      }
    }
  }
}
</script>

<style scoped>
label {
    margin: 0px 0px !important;
}
</style>
